import $mConfig from '@/config/index.config.js';
import http from '@/utils/request/index.js';
import newHttp from '@/utils/request/newRequest.js';
import getTabbr from '@/config/tabbar.js';
import { useUserStore } from './user';
// let SHOP = uni.getStorageSync('shop') || {}
let ADDRESS = uni.getStorageSync('address') || {}

const SHOPTYPE = [
	{ value: 1, label: '酒吧' },
	{ value: 2, label: '餐厅,餐吧' },
	{ value: 3, label: '商超,超市' },
	{ value: 4, label: 'KTV' },
	{ value: 5, label: '便利店' },
	{ value: 6, label: '专营店' }
]
import { defineStore } from 'pinia'
// 第一个参数是你的应用中 Store 的唯一 ID。
export const useShopStore = defineStore('shop', {
	state: () => ({
		shop: uni.getStorageSync('shop') || {},
		shopList: [],
		address: ADDRESS,
		shopType: SHOPTYPE,
		userCardId: {},
		positionInfo: {},
		msgBadge: 0,
		chooseItemId:'',
	}),
	getters: {
	    id: (state) => state.shop.id,
	    logo: (state) => state.shop.logoUrl ? state.shop.logoUrl : 'https://cdn.shan-niang.com/shops/mendiantouxiang.png',
		dropDownShopList: state => {
			const res = state.shopList
			if (!res) return
			let arr = []
			for (let i = 0; i < res.length; i++) {
				arr.push({
					label: res[i].name,
					value: res[i].id
				})
			}
			return arr
		},
	  },
	actions: {
		setShopInfo(info){
			this.shop = info
			uni.setStorageSync('shop', info)
			uni.setStorageSync('last_shop_id', info.id)
		},
		clearShopInfo(){
			this.shop = {}
			this.shopList = []
			uni.removeStorageSync('shop')
		},
		login(list) {
			let USER = uni.getStorageSync('user')
			// 存储店铺列表
			this.shopList = list;
			if(list.length!==0){
				// 判断上一次存储的店铺id
				const lastShopId = uni.getStorageSync('last_shop_id')
				if (!lastShopId) {
					this.switchShop(list[0].id)
				}else{
					const index = list.findIndex(item => {
						return item.id == lastShopId
					})
					let shopId = 0;
					if (index != -1) {
						shopId = list[index].id
					} else {
						shopId = list[0].id
					}
					this.switchShop(shopId);
				}
			}else{
				this.clearShopInfo();
				 this.updateUserMenus('')
				 getTabbr()
			}
		},
		async switchShop(shopId) {
			const res = await http.get('api/Shop/GetShop', { params: { id: shopId } })
			this.setShopInfo(res)
			await this.updateUserMenus(shopId)
			getTabbr()
		},
		// 存储店铺列表
		setShopList(list) {
			this.shopList = list;
		},
		// 修改店铺信息
		updateInfo( data) {
			let info = uni.getStorageSync('shop') || {}
			const keys = Object.keys(data)
			for (let i = 0; i< keys.length; i++) {
				info[keys[i]] = data[keys[i]]
			}
			this.setShopInfo(info);
		},
		// 用户权限更新
		async updateUserMenus(shopId){
			let USER = uni.getStorageSync('user');
			const menu = await newHttp.get('api/User/GetShopUserMenu', { params: { shopId:shopId, userId: USER.id }});
			const role = await newHttp.get('api/User/GetShopUserPowers', { params: { shopId:shopId, userId: USER.id }});
			if(menu.code === 200&&role.code ===200){
				if(menu.data.length !== USER.menuCodes.length){
					USER.menuCodes = menu.data;
					if(role.data){
						USER.isSale = role.data.seller.length!=0;
						USER.isPurchase = role.data.buyer.length!=0;
						USER.isOwner = role.data.brandOwner.length!=0;
					}else{
						USER.isSale = false;
						USER.isPurchase = false;
						USER.isOwner = false;
					}
					uni.setStorageSync('user', USER);
					const userStore  = useUserStore();
					userStore.updateInfo(USER, { root: true});
				}
			}else{
				uni.showToast({
					title:'获取店铺账号权限失败'
				})
			}
			
		},
		async getMessageNum({ commit,dispatch }){
			const res = await newHttp.get('api/Message/GetUnReadMessageQuantity');
			if(res.code === 200){
				this.msgBadge = res.data;
			}else{
				this.msgBadge = 0;
			}
		},
		setAddressInfo(info) {
			this.address = info
			uni.setStorageSync('address', info)
		},
		setPositionInfo(info) {
			this.positionInfo = info
		},
		// 查看员工信息
		setUserCard( info) {
			this.userCard = info
		},
		// 修改消息角标
		setMsgBadge( num) {
			this.msgBadge = num
		},
		setNoShop(){
			this.clearShopInfo()
			let USER = uni.getStorageSync('user');
			USER.menuCodes = [];
			USER.isSale = false;
			USER.isPurchase = false;
			USER.isOwner = false;
			uni.setStorageSync('user', USER);
			const userStore  = useUserStore();
			userStore.updateInfo(USER, { root: true});
		}
	}
})
